<template>
  <div class="dialog">
    <el-dialog title="添加账号"
               :visible.sync="dialogFormVisible.show"
               width="500px">
      <el-form :model="form"
               :rules="rules"
               ref="ruleForm">
        <el-form-item label="账号:"
                      :label-width="formLabelWidth"
                      prop="account">
          <el-input v-model="form.account"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称:"
                      :label-width="formLabelWidth"
                      prop="name">
          <el-input v-model="form.name"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary"
                   @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    dialogFormVisible: Object
  },
  methods: {
    cancel() {
      this.$emit("changeDialogAddVisible");
    },
    async submit() {
      const isValid = this.valid("ruleForm");
      if (!isValid) {
        this.$message({
          message: "请填写正确的信息",
          type: "error",
          center: true
        });
        return;
      }
      this.$emit("submit", this.form);
    },
    valid(formName) {
      let isValid;
      this.$refs[formName].validate(vaild => {
        isValid = vaild;
      });
      return isValid;
    }
  },
  data() {
    return {
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 1, max: 25, message: "长度在 1 到 25 个字符", trigger: "blur" }
        ],
        account: [
          { required: true, message: "请输入账号", trigger: "blur" },
          { min: 1, max: 25, message: "长度在 1 到 25 个字符", trigger: "blur" }
        ]
      },
      form: {
        name: "",
        account: ""
      },
      formLabelWidth: "55px"
    };
  }
};
</script>
<style lang="scss" scoped>
.dialog {
  text-align: center;
}
.input {
  width: "320px";
}
</style>
